const express = require("express");
const router = express.Router();
const User = require("../model/user");
const { randomCode, sendCode } = require("../utils/getMessage");
// const crypto = require('crypto');
// const bodyParser = require("body-parser");
var resData;
router.use(function (req, res, next) {
  resData = {
    status: 0,
    msg: "",
  };
  next();
});

let code;
// 二维码获取
router.post("/eq", (req, res) => {
  var param = req.body;
  var number = param.code;
  console.log(number)
  code = randomCode(6); 
  sendCode(number, code, function (success) {
    if (success) {
      res.send({ msg: "短信验证码已发送", eqcode: code });
    } else {
      res.send("短信验证码发送失败");
    }
  });
});

// 注册
router.post("/register", (req, res) => {
  var param = req.body;
  console.log(param);

  var username = param.username;
  var oldpassword = param.password;
  // 用md5加密
  // var md5Password = crypto.createHash('md5');
  // md5Password.update(oldpassword, 'utf8');
  // var password = md5Password.digest('hex')
  // console.log(md5Password.digest('hex'))

  var codes = param.code;
  if (codes !== code) {
    resData.status = 1;
    resData.msg = "验证码不对呦";
    res.json(resData);
  } else {
    // 查询数据库中有无重复
    User.findOne({
      username: username,
    }).then(function (data) {
      // 如果已存在
      if (data) {
        resData.status = 1;
        resData.msg = "用户名已被注册";
        res.json(resData);
        return;
      } else {
        // 不存在则创建
        var user = new User({
          username: username,
          password: oldpassword,
          realname: username
        });
        resData.status = 0;
        resData.msg = "注册成功";
        resData.datas = {
          _id: user._id,
          username: user.username,
          password: user.oldpassword,
          create_time: user.createdAt,
        };
        res.json(resData);
        return user.save();
      }
    });
  }
});

// 登录
router.post("/login", (req, res) => {
  // console.log(req)
  // res.json({data:'login'},201);
  var param = req.body;
  console.log(param);

  var username = param.username;
  var oldpassword = param.password;
  // var md5 = crypto.createHash('md5');
  // md5.update(oldpassword, 'utf8');
  // var password = md5.digest('hex')
  if (username == "" || oldpassword == "") {
    resData.status = 1;
    resData.msg = "用户名或密码不能为空";
    res.json(resData);
    return;
  }
  // 查询数据库验证用户名和密码
  // console.log(username)
  // console.log(oldpassword)
  User.findOne({
    username: username,
    password: oldpassword,
  }).then(function (data) {
    console.log(data);
    if (!data) {
      resData.status = 1;
      resData.msg = "用户名或密码错误";
      res.json(resData);
      return;
    }
    // 用户名密码正确登录
    resData.status = 0;
    resData.msg = "登录成功";
    resData.datas = {
      _id: data._id,
      username: data.username,
    };
    res.json(resData);
    return;
  });
});
// 账号查询
router.post("/Retrieve", (req, res) => {
  var param = req.body;
  console.log(param);

  var _id = param.id;
  User.findOne(
    {
      _id: _id,
    },
    function (err, txt) {
      if (err) {
        res.json({
          status: "1",
          msg: "用户不存在",
          data: "",
        });
      }
      if (txt) {
        res.json({
          status: "0",
          msg: "查找成功",
          data: txt,
        });
      }
    }
  );
  // res.json({data:'Retrieve'},201);
});

// 修改资料
router.post("/alter", (req, res) => {
  var param = req.body;
  // console.log(param);
  var _id = param.id;
  var change = param.change

  User.findOneAndUpdate({
    _id: _id,
  }, 
  { $set:  
    change
  },
  {},function(err,data){
    if(err){
      resData.status = 1;
      resData.msg = "用户id错误";
      res.json(resData);
    }else if(!data){
      console.log('未找到数据')
    }else if(data){
      console.log('资料修改成功')
      resData.status = 0;
      resData.msg = "资料修改成功";
      res.json(resData);
    }
  }
  )
});

// 修改昵称
router.post("/alterRealname", (req, res) => {
  var param = req.body;
  // console.log(param);
  var _id = param.id;
  var realname = param.realname

  User.findOneAndUpdate({
    _id: _id,
  },
    {
      $set:
        { "realname": realname }
    },
    {}, function (err, data) {
      if (err) {
        resData.status = 1;
        resData.msg = "用户id错误";
        res.json(resData);
      } else if (!data) {
        console.log('未找到数据')
      } else if (data) {
        console.log('资料修改成功')
        resData.status = 0;
        resData.msg = "资料修改成功";
        res.json(resData);
      }
    }
  )
});


module.exports = router;
